import axios from 'axios'
import { config } from './global.config'
import '../plugins/element.js'

// 全局拦截器，每个请求都携带token
axios.interceptors.request.use(conf => {
  conf.headers.Authorization = config.authorization.token
  return conf
})

const env = process.env.NODE_ENV
if (env === 'development') {
  // axios.defaults.baseURL = 'http://localhost:8888/api/private/v1/'
  axios.defaults.baseURL = 'http://192.168.31.172:8888/api/private/v1/'
} else if (process.env.NODE_ENV === 'production') {
  axios.defaults.baseURL = 'https://shop.wlizhi.cc/vue-shop-server/api/private/v1/'
}

export const configAxios = axios

export const failed = function (res, code, alertSuccess) {
  code ||= 200
  if (res.meta.status !== code) {
    this.$message.error(`${res.meta.status} ${res.meta.msg}`)
    return true
  }
  if (alertSuccess) {
    this.$message.success(`${res.meta.status} ${res.meta.msg}`)
  }
  return false
}
